package com.lib.common.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lib.commom.R;
import com.lib.common.views.TitleView;
import com.lib.common.utils.AppManager;
import com.lib.common.utils.LogUtil;
import com.lib.common.utils.NotchUtils;
import com.lib.common.utils.Tips;
import com.lib.common.utils.statusbar.StatusBarCompat;
import com.lib.common.views.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yangju on 2018/11/17.
 */
public abstract class BaseActivity extends AppCompatActivity {
    private Unbinder mUnBinder;
    private TitleView titleView;
    private View emptyView; // 适配notch写的一个空的view
    /***是否显示标题栏*/
    private boolean isShowTitle = false;
    /***是否显示状态栏*/
    private boolean isShowStateBar = false;
    private LoadingDialog loadingDialog;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(!isShowTitleBar()){
            getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN, WindowManager.LayoutParams. FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            ActionBar bar = getSupportActionBar();
            if(bar != null){
                bar.hide();
            }
        }

        setLayoutViewBefore();

        if(setStatusBarTranslate()){
            statusBarTranslate();
            setStatusBarColor(R.color.color_title_bg);
        }

        //设置布局
        if(initLayout() != 0) {
            setContentView(initLayout());
        }

        emptyView = findViewById(R.id.empty_view);
        ImageView back = findViewById(R.id.back);
        if(null != back){
            back.setOnClickListener(v -> finish());
        }

        if(null != getIntent() && null!=getIntent().getExtras()){
            getExtras(getIntent().getExtras());
        }

        mUnBinder = ButterKnife.bind(this);

        titleView = findViewById(R.id.topview);

//        if(setAdaptNotchBar()) {
//            setNotchBarHeight(titleView);
//        }

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        //初始化控件
        initView(savedInstanceState);

        //设置数据
        initData();

        AppManager.getAppManager().addActivity(this);

        LogUtil.e("页面地址: "+this.getClass().getName());
    }

    /** 是否适配刘海 */
    protected boolean setAdaptNotchBar(){
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    protected void setNotchBarHeight(View view) {
        View v = null;
        if(null != view){
            v = view;
        }else {
            if(null != titleView) {
                v = titleView;
            }
        }

        // 如果有刘海
        if (NotchUtils.hasNotchScreen(this) && null != v) {
            int notchHeight = NotchUtils.getNotchHeight(this);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
            params.setMargins(0, notchHeight, 0, 0);
            v.setLayoutParams(params);

            if(null != emptyView){
                emptyView.setVisibility(View.GONE);
            }

        } else {
            if(null != emptyView){
                emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    protected void setLayoutViewBefore(){}

    protected void getExtras(Bundle extras){ }

    protected void setTitle(String title){
        if(null != titleView){
            titleView.setTitle(title);
        }
    }

    protected TitleView getTitleView(){
        return titleView;
    }

    /** 设置颜色 */
    protected void setStatusBarColor(int colorID){
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(colorID));
    }

    public void showLoading(){
        if(null == loadingDialog){
            loadingDialog = new LoadingDialog(this);
        }

        loadingDialog.show();
    }

    public void dismissLoading(){
        if(null != loadingDialog && loadingDialog.isShowing()){
            loadingDialog.dismiss();
        }
    }

    /**
     * 设置布局
     *
     * @return
     */
    public abstract int initLayout();

    /**
     * 初始化布局
     */
    public abstract void initView(Bundle savedInstanceState);

    /**
     * 设置数据
     */
    public abstract void initData();

    /**
     * 是否设置标题栏
     *
     * @return
     */
    public boolean isShowTitleBar() {
        return true;
    }

    /**
     * 设置是否显示状态栏
     * @param isShow
     */
    public void setStateBar(boolean isShow) {
        isShowStateBar = isShow;
    }

    public void showToast(String msg){
        Tips.showTips(this, msg);
    }

    public void showToast(int sourceID){
        Tips.showTips(this, getResources().getString(sourceID));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnBinder.unbind();
        mUnBinder = null;
        dismissLoading();

        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        AppManager.getAppManager().finishActivity(this);
    }

    protected void setTranslucentStatus(boolean on) {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
            return;
        }
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    /** 设置最顶上的状态栏为透明色 */
    protected boolean setStatusBarTranslate() {
        return true;
    }

    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            getWindow().setAttributes(localLayoutParams);
        }
        //修改字体颜色
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//android6.0以后可以对状态栏文字颜色和图标进行修改
//            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
//        }
    }

    /** 设置最顶上的状态栏为透明色 */
    protected void statusBarTranslate(){
        if(Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public View getEmptyView(){
        return emptyView;
    }

    @Subscribe
    public void onEvent(String event){

    }
}
